#!/usr/bin/env python3
from nicegui import ui
from typing import List, Dict, Any


class AgGrid():
    def __init__(self, rows:List[Dict[str, Any]]=[]):
        self.__columns = [
            {'headerName':'住院号','field': 'inpatient_number' },
            {'headerName':'患者姓名','field': 'patient_name' },
            {'headerName':'科室','field': 'department' },
            {'headerName':'出院日期','field': 'discharge_date' },
            {'headerName':'管床医师','field': 'docter' },
            ]
        self.__rows = rows
        self.__contains=ui.row().classes('w-full')
        with self.__contains:
            aggrid = ui.aggrid({
            'columnDefs': self.__columns,
            'rowData': self.__rows,
            'rowSelection': 'multiple',
            # 'stopEditingWhenCellsLoseFocus': True,
            # 'allowTyping':True,
            # 'filterList':True,
        }).on('rowSelected', lambda e: ui.notify(e.args["data"]["inpatient_number"])).classes(
            ).props('borderless')


    def update(self, rows:List[Dict[str, Any]]):
        self.__rows = rows
        self.__contains.update
    
    

# @ui.page('/ag-grid')
# def page():
#     columns = [
#     {'field': 'name', 'editable': False, 'sortable': False},
#     {'field': 'age', 'editable': False},
#     {'field': 'id'},
#     ]
#     rows = [
#         {'id': 3, 'name': 'Alice', 'age': 18},
#         {'id': 4, 'name': 'Bob', 'age': 21},
#         {'id': 5, 'name': 'Carol', 'age': 20},
#     ]

#     # def add_row():
#     #     new_id = max((dx['id'] for dx in rows), default=-1) + 1
#     #     rows.append({'id': new_id, 'name': 'New name', 'age': None})
#     #     ui.notify(f'Added row with ID {new_id}')
#     #     aggrid.update()

#     # def handle_cell_value_change(e):
#     #     new_row = e.args['data']
#     #     ui.notify(f'Updated row to: {e.args["data"]}')
#     #     rows[:] = [row | new_row if row['id'] == new_row['id'] else row for row in rows]

#     async def delete_selected():
#         selected_id = [row['id'] for row in await aggrid.get_selected_rows()]
#         rows[:] = [row for row in rows if row['id'] not in selected_id]
#         ui.notify(f'Deleted row with ID {selected_id}')
#         aggrid.update()

#     aggrid = ui.aggrid({
#         'columnDefs': columns,
#         'rowData': rows,
#         'rowSelection': 'multiple',
#         'stopEditingWhenCellsLoseFocus': True,
#         'allowTyping':True,
#         'filterList':True,
#     }).on('rowSelected', lambda e: e.args["data"]["name"])   #on('cellValueChanged', handle_cell_value_change)

#     # ui.button('Delete selected', on_click=delete_selected)
#     # ui.button('New row', on_click=add_row)


